﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace LCrs.Web.personeinfo
{
    /// <summary>
    /// GetPic 的摘要说明
    /// </summary>
    public class GetPic : IHttpHandler, IRequiresSessionState
    {
        StringBuilder result = new StringBuilder();
        StringBuilder sb = new StringBuilder();
        StringBuilder rechild = new StringBuilder();
        LCrs.BLL.picinfo bll = new LCrs.BLL.picinfo();
        BLL.pictypeinfo bllpic = new BLL.pictypeinfo();
        public void ProcessRequest(HttpContext context)
        {
            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }
            if (ac == "query")
                Query(context);
            else if (ac == "querytree")
                QueryTree(context);
            else if (ac == "add")
                add(context);
            else if (ac == "modify")
                modify(context);
            else if (ac == "delete")
                delete(context);
            else if (ac == "deletes")
                deletes(context);
            else if (ac == "uploadZdtzd")
                uploadZdtzd(context);
            

        }
        
        private string GetTreeJsonByTablea(DataTable tabel, string idCol, string txtCol, int pid)
        {
            result.Remove(0, result.Length);
            if (tabel.Rows.Count > 0)
            {
                result.Append("[");
                int ptid = 0;
                foreach (DataRow row in tabel.Rows)
                {
                    //result.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol].ToString().Trim() + "\",\"type\":\"" + addattr + "\"},");
                    result.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"type\":\"2\",\"iconCls\":\"icon-attach\"");
                    int.TryParse(row["ptid"].ToString(), out ptid);
                    string filer = string.Format("ptid={0} and pid={1}", ptid, pid);
                    DataSet ds = bll.GetList(filer);
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        result.Append(",\"children\":");
                        result.Append("[");
                        sb.Remove(0, sb.Length);
                        foreach (DataRow row1 in ds.Tables[0].Rows)
                        {

                            sb.Append("{\"id\":\"" + row1["picid"] + "\",\"text\":\"" + row1["picname"] + "\",\"bm\":\"" + row1["pfile"] + "\",\"ptid\":\"" + row1["ptid"] + "\",\"type\":\"1\",\"iconCls\":\"icon-picture\"");

                            sb.Append("},");
                        }
                        sb = sb.Remove(sb.Length - 1, 1);

                        result.Append(sb.ToString());
                        result.Append("]");
                    }
                    result.Append("},");
                }
                result = result.Remove(result.Length - 1, 1);
                result.Append("]");

            }
            return result.ToString();
        }
        public void QueryTree(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = Web.Admin.myinfo.getID(context);
                string uname, utruename, st;
                uname = utruename = st = "";
                int pid = 0;
                if (null != context.Request.Form["pid"])
                {
                    pid = int.Parse(context.Request.Form["pid"].ToString().Trim());
                }                
                StringBuilder strWhere = new StringBuilder();
                if (pid != 0)
                {
                    strWhere.Append(" pid=" + pid);
                }
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                
                //调用分页的GetList方法  
                //DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                //int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                //string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                DataSet ds = bllpic.GetList("1=1 order by ptnum");
                string str = GetTreeJsonByTablea(ds.Tables[0], "ptid", "ptname",pid);                
                context.Response.Write(str);//返回给前台页面  
                context.Response.End();
            }

        }
        public void deletes(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                string uidstr = context.Request.QueryString["uids"];
                if (bll.DeleteList(uidstr))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();

                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = 0;
                string uidstr = context.Request.QueryString["uid"];
                if (uidstr != "")
                    uid = int.Parse(uidstr);
                if (bll.Delete(uid))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }

        public void modify(HttpContext context)
        {
            if (Web.Admin.myinfo.isLogin(context))
            {
                context.Response.ContentType = "text/plain";
                int pid = 0;
                if (context.Request.Form["picid"] != null)
                    int.TryParse(context.Request.Form["picid"], out pid);
                if (pid != 0)
                {
                    string htpic = context.Request.Form["htpic"];
                    string picname = context.Request.Form["picname"];
                    string ptids = context.Request.Form["ptid"];
                    int ptid = 0;
                    int.TryParse(ptids, out ptid);
                    string savepath = "";
                    HttpFileCollection files = context.Request.Files;
                    if (files.Count == 1)
                    {
                        HttpPostedFile file = files[0];
                        if (file.ContentLength > 0)
                        {
                            //全路径  
                            string FullFullName = file.FileName;
                            //获取图片的名称  
                            String fileName = FullFullName.Substring(FullFullName.LastIndexOf("\\") + 1);
                            string exp = fileName.Substring(fileName.LastIndexOf('.')).ToLower();
                            string filenames = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                            string path = "~/uppics";
                            savepath = "/uppics";
                            if (exp == ".jpg" || exp == ".bmp" || exp == ".gif" || exp == ".png")
                            {
                                file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(path) + "\\" + filenames + exp);
                                savepath += "/" + filenames + exp;
                            }
                            else
                            {
                                context.Response.Write("P");//返回给前台页面  
                                context.Response.End();

                            }
                        }
                        else
                        {
                            savepath = htpic;
                        }
                    }
                    else
                    {
                        savepath = htpic;
                    }
                    LCrs.Model.picinfo model = new LCrs.Model.picinfo();                    
                    model.ptid = ptid;
                    model.picname = picname;
                    model.pfile = savepath;
                    model.picid = pid;
                    if (bll.Update(model))
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();
                    }
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }

        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int pid = 0;
                if (context.Request.QueryString["pid"] != null)
                    int.TryParse(context.Request.QueryString["pid"], out pid);
                if (pid != 0)
                {


                    DateTime pdate = DateTime.Now;                   
                    string picname = context.Request.Form["picname"];
                    string ptids = context.Request.Form["ptid"];
                    int ptid = 0;
                    int.TryParse(ptids, out ptid);
                    string savepath = "";
                    HttpFileCollection files = context.Request.Files;
                    if (files.Count == 1)
                    {
                        HttpPostedFile file = files[0];
                        if (file.ContentLength > 0)
                        {
                            //全路径  
                            string FullFullName = file.FileName;
                            //获取图片的名称  
                            String fileName = FullFullName.Substring(FullFullName.LastIndexOf("\\") + 1);
                            string exp = fileName.Substring(fileName.LastIndexOf('.')).ToLower();
                            string filenames = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                            string path = "~/uppics";
                            savepath = "/uppics";
                            if (exp == ".jpg" || exp == ".bmp" || exp == ".gif" || exp == ".png")
                            {
                                file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(path) + "\\" + filenames + exp);
                                savepath += "/" + filenames + exp;
                            }
                            else
                            {
                                context.Response.Write("P");//返回给前台页面  
                                context.Response.End();

                            }
                        }
                    }
                    LCrs.Model.picinfo model = new LCrs.Model.picinfo();
                    model.pdate = pdate;
                    model.ptid = ptid;
                    model.picname = picname;
                    model.pfile = savepath;
                    model.pid = pid;
                    int rid = bll.Add(model);
                    if (rid > 0)
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();
                    }
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }

        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = Web.Admin.myinfo.getID(context);
                string uname, utruename, st;
                uname = utruename = st = "";
                int pid = 0;
                if (null != context.Request.Form["pid"])
                {
                    pid = int.Parse(context.Request.Form["pid"].ToString().Trim());
                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (pid != 0)
                {
                    strWhere.Append(" pid=" + pid);
                }
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }

                //调用分页的GetList方法  
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }


        public void uploadZdtzd(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int pid = 0;
                if (context.Request.QueryString["pid"] != null)
                    int.TryParse(context.Request.QueryString["pid"], out pid);
                if (pid != 0)
                {
                   
                    DateTime pdate = DateTime.Now;

                    string savepath = "";
                    HttpFileCollection files = context.Request.Files;
                    if (files.Count == 1)
                    {
                        HttpPostedFile file = files[0];
                        if (file.ContentLength > 0)
                        {
                            //全路径  
                            string FullFullName = file.FileName;
                            //获取图片的名称  
                            String fileName = FullFullName.Substring(FullFullName.LastIndexOf("\\") + 1);
                            string exp = fileName.Substring(fileName.LastIndexOf('.')).ToLower();
                            string filenames = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                            string path = "~/uppics";
                            savepath = "/uppics";

                            file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(path) + "\\" + filenames + exp);
                            savepath += "/" + filenames + exp;

                            LCrs.BLL.PersonInfo pm = new BLL.PersonInfo();

                            LCrs.Model.PersonInfo p = pm.GetModel(pid);

                            p.status = 1;
                            p.remark = p.remark + "|" + filenames + exp;//把调函存到remark中

                            pm.Update(p);

                            #region
                            //if (exp == ".jpg" || exp == ".bmp" || exp == ".gif" || exp == ".png")
                            //{
                            //    file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(path) + "\\" + filenames + exp);
                            //    savepath += "/" + filenames + exp;

                            //    LCrs.BLL.PersonInfo pm = new BLL.PersonInfo();

                            //    LCrs.Model.PersonInfo p = pm.GetModel(pid);

                            //    p.status = 1;
                            //    p.remark = p.remark + "|" + filenames + exp;//把调函存到remark中

                            //    pm.Update(p);
                            //}
                            //else
                            //{
                            //    context.Response.Write("P");//返回给前台页面  
                            //    context.Response.End();

                            //}
                            #endregion
                        }
                        else
                        {
                            LCrs.BLL.PersonInfo pm = new BLL.PersonInfo();

                            LCrs.Model.PersonInfo p = pm.GetModel(pid);

                            p.status = 1;

                            pm.Update(p);
                        }
                    }
                    
                    int rid = 1;
                    if (rid > 0)
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();
                    }
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}